<template>
  <div class="mainbox">
    <div class="textinput">
      <SearchInput
        :placeholder="placeholder"
        :page.sync="pageInfo.page"
        @handleSearch="init"
      >
      </SearchInput>
      <el-button
        icon="el-icon-plus"
        type="primary"
        @click="$router.push({ name: 'AddEmployee', query: { isEdit: false } })"
        >添加成员</el-button
      >
    </div>
    <div class="messagetable">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="name" label="员工姓名"> </el-table-column>
        <el-table-column prop="username" label="账号"> </el-table-column>
        <el-table-column prop="phone" label="手机号"> </el-table-column>
        <el-table-column prop="status" label="账号状态">
          <template slot-scope="scope">
            {{ scope.row.status === 1 ? "正常" : "已禁用" }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-link
              type="primary"
              @click="
                $router.push({
                  name: 'EditEmployee',
                  query: { id: scope.row.id, isEdit: true },
                })
              ">编辑</el-link
            >
            <el-divider direction="vertical"></el-divider>
            <el-popconfirm
              title="确认调整该账号的状态?"
              @onConfirm="handleChangeStatus(scope.row)"
            >
              <el-link slot="reference" type="danger">{{
                scope.row.status === 1 ? "禁用" : "启用"
              }}</el-link> 
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <Pagination  
    @pagination="init"
    :total="total"
    :page.sync="pageInfo.page"
    :limit.sync="pageInfo.pageSize"
    />
  </div>
</template>

<script>
import { getEmployee, changeEmployeeStatus } from "@/api/employee";
import Pagination from "@/components/Pagination"
import SearchInput from "@/components/SearchInput"
export default {
  components:{
    Pagination,
    SearchInput,
  },
  data() {
    return {
      placeholder:'请输入员工姓名',
      tableData: [],
      pageInfo: {
        page: 1,
        pageSize: 10,
      },
      total: 0,
    };
  },
  created() {
    this.init();
  },
  methods: {
    init(names) {
      this.name=names
      let params;
      if (this.name) {
        params = { ...this.pageInfo, name: this.name };
      } else {
        params = { ...this.pageInfo };
      }
      getEmployee(params).then((res) => {
        this.tableData = res.data.records;
        this.total = res.data.total;
      });
    },
    handleChangeStatus(row) {
      const data = {
        id: row.id,
        status: +!row.status,
      };
      changeEmployeeStatus(data).then(() => {
        this.$message.success("改变账号状态成功");
        this.init();
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.mainbox {
  .textinput {
    display: flex;
    justify-content: space-between;
  }
  .messagetable {
    margin-top: 10px;
  }
}
</style>
